import re

from aiogram import Bot, types
from aiogram_dialog import DialogManager
from fluentogram import TranslatorRunner

from poker_bot.config import Settings
from poker_bot.core.models.enums.currency import Currency
from poker_bot.core.models.enums.user import UserRole
from poker_bot.database.services import (
    PaymentMethodService,
    UserService,
    WithdrawService,
)
from poker_bot.tg_bot.keyboards.inline import withdraw_confirmation_kb
from poker_bot.tg_bot.services.tg_helpers import (
    answer_msg_with_autodelete,
    broadcast,
    send_tg_message,
)
from poker_bot.tg_bot.validators.card import validate_credit_card


async def set_payment_amount(
    message: types.Message, _, manager: DialogManager, payment_amount: str
):
    locale: TranslatorRunner = manager.middleware_data.get("locale")
    try:
        payment_amount = float(payment_amount)
    except ValueError:
        await answer_msg_with_autodelete(message, locale.get("wrong-sum-format-msg"))
        return
    manager.dialog_data["payment_amount"] = payment_amount
    await manager.next()


async def create_withdraw(
    message: types.Message, __, manager: DialogManager, payment_address: str
):
    """Создаем запрос на вывод"""

    bot: Bot = manager.middleware_data.get("bot")
    locale: TranslatorRunner = manager.middleware_data.get("locale")
    config: Settings = manager.middleware_data.get("config")
    withdraw_service: WithdrawService = manager.middleware_data.get("withdraw_service")
    user_service: UserService = manager.middleware_data.get("user_service")
    payment_method_service: PaymentMethodService = manager.middleware_data.get(
        "payment_method_service"
    )

    payment_amount: float = manager.dialog_data.get("payment_amount")
    payment_method_id: str = manager.dialog_data.get("payment_method_id")

    payment_method = await payment_method_service.get_payment_method_by_id(
        int(payment_method_id)
    )
    if payment_method.currency != Currency.usdt:
        payment_address = payment_address.strip().replace(" ", "")
        if not validate_credit_card(payment_address):
            await answer_msg_with_autodelete(
                message, locale.get("wrong-card-format-msg")
            )
            return

    user = await user_service.get_user_by_telegram_id(message.from_user.id)
    withdraw = await withdraw_service.create_withdraw(
        user_tg_id=message.from_user.id,
        payment_address=payment_address,
        summ=float(payment_amount),
        payment_method_id=int(payment_method_id),
    )
    withdraw_id = withdraw.id
    admins = await user_service.get_users_with_role(UserRole.admin)
    await broadcast(
        bot,
        admins,
        locale.get("new-withdraw-for-confirmation-msg", withdraw_id=withdraw_id),
        reply_markup=withdraw_confirmation_kb(locale, withdraw_id=withdraw_id),
    )
    await send_tg_message(
        bot,
        config.tg_bot.admin_channel_id,
        locale.get(
            "new-withdraw-msg",
            withdraw_id=withdraw_id,
            username=user.username,
            withdraw_summ=str(withdraw.summ),
            currency_symbol=payment_method.currency_symbol.value,
        ),
    )
    await message.answer(locale.get("moderation-msg"))
    await manager.done()
